package hw;


import Util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Changemoney extends JPanel implements ActionListener {
    private Atm atm;
    private Icon sureImg = Util.getIcon("/buttonSure.jpg");
    private Icon offImg = Util.getIcon("/buttonOff.jpg");
    private JButton sureBtn = new JButton(sureImg);
    private JButton offBtn = new JButton(offImg);
    private JTextField changename = new JTextField();
    private JTextField changemoney = new JTextField();

    public Changemoney(Atm atm) {
        this.atm = atm;
        offBtn.setBounds(930,200,140,50);
        sureBtn.setBounds(680,200,140,50);
        changename.setBounds(430,200,240,50);
        changemoney.setBounds(430,300,240,50);
        //添加组件
        this.add(offBtn);
        this.add(sureBtn);
        this.add(changename);
        this.add(changemoney);

        //监听绑定
        offBtn.addActionListener(this);
        sureBtn.addActionListener(this);

        this.setLayout(null);
        this.setOpaque(false);

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        Users use = atm.getUser();
        if (e.getSource() == offBtn) {
            atm.switchPanel("menu");
        }
        if (e.getSource()==sureBtn){
             if (changename.getText().equals(use.getName())){
                 JOptionPane.showConfirmDialog(this,"不能转账给自己");
                 return;
             };
             if (Integer.parseInt(changemoney.getText())>use.getBalance()){
                 JOptionPane.showConfirmDialog(this,"金额不足");

                 return;
             }
            System.out.println(changename.getText());
             for (int i =0;i<atm.getUseList().size();i++){
                 if (changename.getText().equals(atm.getUseList().get(i).getName())){
                     System.out.println(1);
                     use.setBalance(use.getBalance()-Integer.parseInt(changemoney.getText()));
                     atm.getUseList().get(i).setBalance(atm.getUseList().get(i).getBalance()+Integer.parseInt(changemoney.getText()));
                 }
             }

        }


    }
}
